<template>
    <a-modal :keyboard="false" :maskClosable="false" title="强制关单" :open="visible" :width="550" :confirmLoading="loading" centered @ok="handleSubmit" @cancel="handleCancel">
        <a-form layout="vertical">
            <a-row :gutter="16" type="flex" justify="start">
                <a-col :xs="24" :sm="24" :md="24">
                    <a-form-item label="强制关单说明" v-bind="validateInfos.content">
                        <a-textarea v-model:value="modelRef.content" show-count :maxlength="500"></a-textarea>
                    </a-form-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="24">
                    <a-form-item label="相关附件" v-bind="validateInfos.forceCloseFiles">
                        <a-image-preview-group>
                            <a-space>
                                <div v-for="(item, index) in followFileList" :key="index" style="margin-bottom: 10px">
                                    <audio controls :src="item.viewAddress" v-if="item.oldName.includes('.mp3') || item.oldName.includes('.m4a')"></audio>
                                    <a-button v-else-if="item.oldName.includes('.mp4')" @click="previewVideo(item)">点击查看视频</a-button>
                                    <a-image v-else :width="100" :height="100" :src="item.viewAddress" />
                                </div>
                            </a-space>
                        </a-image-preview-group>
                        <!-- <custom-upload v-model:fileList="modelRef.forceCloseFiles" upload-text="上传照片、视频、音频" accept="image/*,video/*,audio/*"></custom-upload> -->
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
    </a-modal>
</template>

<script lang="ts">
    import type { PropType } from 'vue';
    import { defineComponent, reactive, ref, watchEffect, watch } from 'vue';
    import { Form, message } from 'ant-design-vue';
    import { cloneDeep, isEmpty } from 'lodash-es';
    import { forceCloseOrder } from '@/api/order/mine-list';

    export default defineComponent({
        props: {
            visible: {
                type: Boolean,
                required: true,
            },
            model: {
                type: Object as PropType<any>,
                default: () => null,
            },
        },
        emits: ['cancel', 'success'],
        setup(props, { emit }) {
            const loading = ref(false);
            const followFileList = ref([]);

            const defaultForm = {
                content: '',
                forceCloseFiles: [],
            };
            const modelRef = reactive<any>(cloneDeep(defaultForm));
            const rulesRef = reactive({
                content: [{ required: true, message: '请选择' }],
                // forceCloseFiles: [{ required: true, message: '请上传', type: 'array' }],
            });
            const { validateInfos, resetFields, validate } = Form.useForm(modelRef, rulesRef);

            // 记录初始值
            const initValues = reactive({});
            watch(
                () => props.visible,
                () => {
                    Object.assign(initValues, cloneDeep(props.model));
                },
            );
            watchEffect(() => {
                if (props.visible) {
                    if (props.model) {
                        Object.assign(modelRef, {
                            orderId: props.model.id,
                        });
                        followFileList.value = [];
                        const list = props.model.recordVO.filter(item => item.type == 6);
                        list.map(item => {
                            followFileList.value = followFileList.value.concat(item.content.followRecord);
                        });
                    }
                }
            });

            const handleCancel = () => {
                resetFields();
                Object.keys(modelRef).forEach(key => {
                    delete modelRef[key];
                });
                Object.assign(modelRef, cloneDeep(defaultForm));

                modelRef.status = 1;
                emit('cancel');
            };

            const handleSubmit = async () => {
                validate().then(() => {
                    loading.value = true;
                    modelRef.forceCloseFiles = modelRef.forceCloseFiles.concat(followFileList.value);
                    forceCloseOrder({
                        ...modelRef,
                    })
                        .then(res => {
                            if (res.data) {
                                message.success('操作成功');
                                emit('success');
                                handleCancel();
                            }
                        })
                        .finally(() => {
                            loading.value = false;
                        });
                });
            };
            const previewVideo = ({ viewAddress }) => {
                window.open(viewAddress, '_blank');
            };
            return {
                previewVideo,
                followFileList,
                loading,

                modelRef,
                validateInfos,
                resetFields,

                handleSubmit,
                handleCancel,

                initValues,
                isEmpty,
            };
        },
        components: {},
    });
</script>
